

//继承自EventBase的nativeManager类

import EventBase from "../eventBase";
import ttManager from "../native/ttManager";
import wxManager from "../native/wxManager";
import GameMgr from "./gameManager";


const g_adType = {
    open: 'open',
    banner: 'banner',
    reward: 'reward',
    full: 'full',
    express: 'express',
    feed: 'feed',


}


const g_appIdInfo = {
    "com.hapi.snake": {
        open: {
            ad_id: '103068390',
            openId: '889717509',
        },
        banner: {
            ad_id1: '103069644',
            ad_id2: '103084672',//暂时不用
        },
        reward: {
            redPack: '103070623',
            prize: '103070623',

        },
        full: {
            ad_id: '103070114',
        },
        express: {
            hall: '103069743',
            game: '103069743',
        },
        feed: {
            ad_id: '103086887',
        }

    },
    "com.hapi.world": {
        open: {
            ad_id: '103110782',
            openId: '889848536',
        },
        banner: {
            ad_id1: '103110042',
            ad_id2: '103110042',//暂时不用
        },
        reward: {
            redPack: '103110875',
            prize: '103110043',

        },
        full: {
            ad_id: '103108685',
        },
        express: {
            hall: '103111160',
            game: '103110693',
        },
        feed: {
            ad_id: '103110781',
        }

    },
    "com.jumeng.together": {
        open: {
            ad_id: '103119897',
            openId: '889871891',
        },
        banner: {
            ad_id1: '103119991',
            ad_id2: '103119991',//暂时不用
        },
        reward: {
            redPack: '103120178',
            prize: '103120356',

        },
        full: {
            ad_id: '103119992',
        },
        express: {
            hall: '103119898',
            game: '103120179',
        },
        feed: {
            ad_id: '103120355',
        }

    },
}


export const g_url_appid = {
    "com.hapi.snake": "https://download.hapcooge.com/",
    "com.hapi.world": "https://world.hapcooge.com/index.html",
    "com.jumeng.together": "https://world2.hapcooge.com/index.html"
}



export default class NativeMgr extends EventBase {
    private static instance: NativeMgr;
    private wxManager: wxManager;
    public ttManager: ttManager;


    public _deviceId: string;
    public _appId: string;
    public _channel: string;


    private constructor() {
        super();


    }

    public static Ins() {
        if (!this.instance) {
            this.instance = new NativeMgr();
            this.instance.init()
        }
        return this.instance;
    }

    init() {
        this.wxManager = wxManager.getInstance();
        this.ttManager = ttManager.getInstance();
    }

    public emitPlantformMsg(event: string, data: any) {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            NativeMgr.Ins().emitTarget('wx', 'wxMsg', { event, data });
        } else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            NativeMgr.Ins().emitTarget('tt', 'ttMsg', { event, data });
        }
    }



    loginCode() {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            this.wxManager.loginCode()
        } else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            this.ttManager.loginCode()
        }
    }


    initPlantForm() {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            // this.wxManager.getUserInfo(false,(res)=>{
            //     console.log('initPlantForm getUserInfo:',res);
            //     this.emitPlantformMsg('getUserInfo',res);
            // })
        } else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {

        }

    }

    getUserInfo(auth) {
        console.log('auth = ', auth)
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            this.wxManager.getUserInfo(auth)
        } else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            this.ttManager.getUserInfo(auth)
        }
    }


    shareGame(query: string = '') {

        GameMgr.Ins().showMsg('分享游戏')
        console.log('分享游戏')
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {

            this.wxManager.showShare(query)
        } else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            this.ttManager.showShare(query)
        }

    }

    getShareInfo() {
        console.log('获取分享信息')
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            this.wxManager.getShareInfo()
        } else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            this.ttManager.getShareInfo()
        }
    }


    getCheckScene(cb) {
        if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            this.ttManager.checkScene((isCheckScene) => {
                cb(isCheckScene)
            })
        } else {
            cb(false);
        }
    }

    navigateToScene() {
        if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            this.ttManager.navigateToScene()
        } else {

        }

    }

    getAdNumberByType(type) {
        const bundleName = this.appId();
        return g_appIdInfo[bundleName][type];

    }

    //激励广告 1=红包  2=抽奖
    showAds(number: Number) {
        if (cc.sys.platform === cc.sys.ANDROID) {
            GameMgr.Ins().block();
            let id = '102997487'
            const info = this.getAdNumberByType(g_adType.reward);
            if (number == 1) {
                id = info.redPack;
            } else if (number == 2) {
                id = info.prize;
            }
            console.log('showAds !!', id);
            GameMgr.Ins().checkCanContinue(() => {
                jsb.reflection.callStaticMethod('org/cocos2dx/javascript/MediationRewardActivity', 'addAd', "(Ljava/lang/String;)V", id);
            })
        } else {
            console.log('showAds !!', cc.sys.platform);
            cc.systemEvent.emit('RewardArrived', 'ok');
            cc.systemEvent.emit('getEcpm', "openAds_1383.866_pangle_null_889717509_2_null_aab1fac9-e4e5-45e8-9b91-04a54b93659fu4621_splash__null_0___{}");
        }

    }



    showBottomBannerAds(index: number = 1) {

        if (cc.sys.platform === cc.sys.ANDROID) {
            const info = this.getAdNumberByType(g_adType.banner);
            let ad_id = info.ad_id1;
            if (index !== 1) {
                ad_id = info.ad_id2;
            }
            console.log('showBottomBannerAds !!', ad_id);

            GameMgr.Ins().checkCanContinue(() => {
                if (index == 1) {
                    jsb.reflection.callStaticMethod('org/cocos2dx/javascript/MediationRewardActivity', 'addBannerAds', "(Ljava/lang/String;)V", ad_id);
                } else {
                    jsb.reflection.callStaticMethod('org/cocos2dx/javascript/MediationRewardActivity', 'addBannerAds2', "(Ljava/lang/String;)V", ad_id);
                }
            })

        } else {
            console.log('showBottomBannerAds !!', cc.sys.platform);
            cc.systemEvent.emit('getEcpm', "openAds_1383.866_pangle_null_889717509_2_null_aab1fac9-e4e5-45e8-9b91-04a54b93659fu4621_splash__null_0___{}");
        }
    }

    delBottomBannerAds(index: number = 1) {
        if (cc.sys.platform === cc.sys.ANDROID) {
            GameMgr.Ins().checkCanContinue(() => {
                if (index == 1) {
                    jsb.reflection.callStaticMethod('org/cocos2dx/javascript/MediationRewardActivity', 'deletBannerAds', "()V");
                } else {
                    jsb.reflection.callStaticMethod('org/cocos2dx/javascript/MediationRewardActivity', 'deletBannerAds2', "()V");
                }
            })


        } else {
            console.log('delBottomBannerAds !!', cc.sys.platform);
            // cc.systemEvent.emit('openAdsOver','ok');
        }
    }

    //信息流广告
    showFeedAds() {

        if (cc.sys.platform === cc.sys.ANDROID) {
            const info = this.getAdNumberByType(g_adType.feed);
            const ad_id = info.ad_id;

            console.log('showFeedAds !!', ad_id);

            GameMgr.Ins().checkCanContinue(() => {

                jsb.reflection.callStaticMethod('org/cocos2dx/javascript/MediationRewardActivity', 'addFeedAds', "(Ljava/lang/String;)V", ad_id);

            })




        } else {
            console.log('showBottomBannerAds !!', cc.sys.platform);
            cc.systemEvent.emit('getEcpm', "openAds_1383.866_pangle_null_889717509_2_null_aab1fac9-e4e5-45e8-9b91-04a54b93659fu4621_splash__null_0___{}");
        }
    }

    delFeedAds() {
        if (cc.sys.platform === cc.sys.ANDROID) {
            GameMgr.Ins().checkCanContinue(() => {

                jsb.reflection.callStaticMethod('org/cocos2dx/javascript/MediationRewardActivity', 'deletFeedAds', "()V");

            })


        } else {
            console.log('delBottomBannerAds !!', cc.sys.platform);
            // cc.systemEvent.emit('openAdsOver','ok');
        }
    }




    showOpenAds() {


        if (cc.sys.platform === cc.sys.ANDROID) {

            const info = this.getAdNumberByType(g_adType.open);
            const ad_id = info.ad_id;
            const openId = info.openId;
            console.log('showOpenAds !!', ad_id, openId);
            GameMgr.Ins().checkCanContinue(() => {
                jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'addOpenAd', "(Ljava/lang/String;Ljava/lang/String;)V", ad_id, openId);
            })

        } else {
            console.log('showOpenAds !!', cc.sys.platform);
            cc.systemEvent.emit('openAdsOver', 'ok');
            cc.systemEvent.emit('getEcpm', "openAds_1383.866_pangle_null_889717509_2_null_aab1fac9-e4e5-45e8-9b91-04a54b93659fu4621_splash__null_0___{}");
        }
    }

    //全屏广告-游戏内退出
    showFullAds() {

        if (cc.sys.platform === cc.sys.ANDROID) {
            const info = this.getAdNumberByType(g_adType.full);
            const ad_id = info.ad_id;
            console.log('showFullAds !!', ad_id);
            GameMgr.Ins().checkCanContinue(() => {
                jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'addFullAd', "(Ljava/lang/String;)V", ad_id);
            })
        } else {
            console.log('showOpenAds !!', cc.sys.platform);
            // cc.systemEvent.emit('openAdsOver', 'ok');
            cc.systemEvent.emit('getEcpm', "openAds_1383.866_pangle_null_889717509_2_null_aab1fac9-e4e5-45e8-9b91-04a54b93659fu4621_splash__null_0___{}");
        }
    }

    //插屏广告 0=大厅 1-4 游戏内
    showExpressAds(index: number) {

        if (cc.sys.platform === cc.sys.ANDROID) {
            let ad_id = ""
            const info = this.getAdNumberByType(g_adType.express);
            if (index == 0) {
                ad_id = info.hall;
            } else {
                ad_id = info.game;
            }

            console.log('showExpressAds !!', ad_id);
            GameMgr.Ins().checkCanContinue(() => {
                jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'addExpressAd', "(Ljava/lang/String;)V", ad_id);
            })
        } else {
            console.log('showOpenAds !!', cc.sys.platform);
            setTimeout(() => {

                cc.systemEvent.emit('getEcpm', "openAds_1383.866_pangle_null_889717509_2_null_aab1fac9-e4e5-45e8-9b91-04a54b93659fu4621_splash__null_0___{}");

            }, 1000)
            // cc.systemEvent.emit('openAdsOver', 'ok');
        }
    }






    deviceId() {
        if (this._deviceId) {
            return this._deviceId;
        }
        if (cc.sys.platform === cc.sys.ANDROID) {
            this._deviceId = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/Device', 'DeviceId', "()Ljava/lang/String;");
            return this._deviceId;
        } else {
            return '123456789'
        }
    }

    appId() {
        if (this._appId) {
            return this._appId;
        }
        if (cc.sys.platform === cc.sys.ANDROID) {
            this._appId = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/Device', 'ApplicationID', "()Ljava/lang/String;");
            return this._appId;
        } else {
            return 'com.hapi.snake'
        }
    }

    channel() {
        if (this._channel) {
            return this._channel;
        }
        if (cc.sys.platform === cc.sys.ANDROID) {
            this._channel = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getChannel', "()Ljava/lang/String;") || 'csj';
            return this._channel

        } else {
            return 'com.hapi.snake'
        }
    }



    /**
     * 
     * @returns 获取手机状态  开发者 usb 无障碍 root
     */
    getPhoneState() {
        if (cc.sys.platform === cc.sys.ANDROID) {

            return jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getPhoneState', "()Ljava/lang/String;");

        } else {
            return '0_0_0_0'
        }
    }

    /**
     * 
     * @returns 更新定位信息
     */
    updateLocation() {
        if (cc.sys.platform === cc.sys.ANDROID) {

            return jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'updateLocation', "()Ljava/lang/String;");

        } else {
            return '1'
        }
    }

    /**
     * 
     * @returns 获取定位坐标
     */
    getLocation() {
        if (cc.sys.platform === cc.sys.ANDROID) {

            return jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getLocation', "()Ljava/lang/String;");

        } else {
            return '123_345'
        }
    }


    shareUrl(url, keys, values) {
        if (cc.sys.platform === cc.sys.ANDROID) {

            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'shareUrl', "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", url, keys, values);

        } else {
            console.log('设置成功');
        }
    }


    getInviteInfo() {
        if (cc.sys.platform === cc.sys.ANDROID) {

            return jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getInviteInfo', "()Ljava/lang/String;");

        } else {
            console.log('设置成功');
            return ''
        }
    }

}